package com.cw.demo.api.impl;

import java.util.List;

import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheConfig;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.cw.demo.api.UserApi;
import com.cw.demo.api.dto.UserDto;
import com.cw.demo.converter.UserConverter;
import com.cw.demo.dao.model.UserDo;
import com.cw.demo.service.UserService;
import com.cw.starter.center.constant.Constants;
import com.cw.starter.center.dto.BasePageReqDto;
import com.cw.starter.center.dto.BaseReqDto;
import com.cw.starter.center.dto.R;

import lombok.extern.slf4j.Slf4j;

/**
 * 用户接口实现
 *
 * @author chenw
 * @date 2021/7/27
 */
@RestController
@RequestMapping("user/v1")
@DubboService(version = "${dubbo.service.version}", group = "${dubbo.application.name}", protocol = "${dubbo.protocol.name}")
@Slf4j
@CacheConfig(cacheNames = "caffeineCacheManager")
public class UserApiImpl implements UserApi {
    @Autowired
    private UserService userService;

    /**
     * 根据id查询用户信息
     *
     * @param userDto
     * @return
     * @CachePut 不检查是否存在，直接保存
     * @CacheEvict 移除
     */
    @GetMapping("/queryById")
    @Cacheable(value = "userDto", key = "#userDto.id")
    @Override
    public R<UserDto> queryById(BaseReqDto baseReqDto, UserDto userDto) {
        log.info("从数据库查");
        UserDo userDo = userService.getById(userDto.getId());
        return R.ok(UserConverter.do2Dto(userDo), Constants.QUERY_SUCCESS);
    }

    /**
     * 查询用户列表
     *
     * @param userDto
     * @return
     */
    @GetMapping("/queryList")
    @Override
    public R<List<UserDto>> queryList(BaseReqDto baseReqDto, UserDto userDto) {
        return R.ok((userService.queryList(userDto)), Constants.QUERY_SUCCESS);
    }

    /**
     * 分页查询用户列表
     *
     * @param userDto
     * @param basePageReqDto
     */
    @GetMapping("/queryPage")
    @Override
    public R<Page<UserDto>> queryPage(BasePageReqDto basePageReqDto, UserDto userDto) {
        return R.ok((userService.queryPage(userDto, basePageReqDto.getPageNum(), basePageReqDto.getPageSize())),
            Constants.QUERY_SUCCESS);
    }

    /**
     * 新增或修改用户信息
     *
     * @param userDto
     */
    @GetMapping("/saveUser")
    @CachePut(key = "#userDto.id")
    @Override
    public R<Boolean> saveUser(BaseReqDto baseReqDto, UserDto userDto) {
        return R.ok((userService.saveOrUpdate(UserConverter.dto2Do(userDto))), Constants.HANDLE_SUCCESS);
    }

    /**
     * 删除用户信息
     *
     * @param userDto
     */
    @PostMapping("/removeUser")
    @CacheEvict(value = "userDto", key = "#userDto.id")
    @Override
    public R<Boolean> removeUser(BaseReqDto baseReqDto, UserDto userDto) {
        return R.ok((userService.removeUser(userDto)), Constants.HANDLE_SUCCESS);
    }
}
